﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeatheRx.DomainModel.Search;
using WeatheRx.Managers;
using WeatheRx.Managers.Search;
using WeatheRx.Web.Models;
using WeatheRx.Web.Models.Search;

namespace WeatheRx.Web.Controllers
{
    public class SearchController : Controller
    {
        private IApplicantManager applicantManager { get; set; }

        public SearchController()
        {
            applicantManager = new ApplicantManager();
        }

        public SearchController(IApplicantManager am)
        {
            applicantManager = am;
        }

        // GET: Search
        public ActionResult Index()
        {
            var model = new ComprehensiveSearchRequestModel();
            return View(model);
        }

        public ActionResult Navbar(string searchTerm)
        {
            var results = applicantManager.SearchWithMatchCategories(searchTerm);

            var model = new SearchResultsPageViewModel()
            {
                Applicants = Mapper.Map<List<ApplicantSearchResultViewModel>>(results),
                SearchTerm = searchTerm
            };
            return PartialView(model);
        }

        [HttpPost]
        public PartialViewResult Results(ComprehensiveSearchRequestModel search)
        {
            var results = applicantManager.ComprehensiveSearch(search);
            var model = new SearchResultsPageViewModel()
            {
                Applicants = Mapper.Map<List<ApplicantSearchResultViewModel>>(results),
                SearchTerm = search.LastName
            };
            return PartialView(model);
        }
    }
}